package com.buaa.airticketbookingsystem.Controller;

import com.buaa.airticketbookingsystem.Entity.PurchaseRecord;
import com.buaa.airticketbookingsystem.Entity.Ticket;
import com.buaa.airticketbookingsystem.Service.PurchaseRecordService;
import com.buaa.airticketbookingsystem.Service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.Entity;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
public class BuyController {

    @Autowired
    private TicketService ticketService;

    @Autowired
    private PurchaseRecordService purchaseRecordService;

    @PostMapping("/buy")
    public Map<String, Object> preBuy(@RequestBody Map<String, Object> remap){
        String email = (String) remap.get("email");
        String f_id = (String) remap.get("flight_id");
        String date = (String) remap.get("date");
        String time = (String) remap.get("time");
        List<String> s_idList = (ArrayList<String>) remap.get("seatList");
        List<String> prList = new ArrayList<>();
        Map<String, Object> resMap = new HashMap<>();
        for(String s_id : s_idList){
            String t_id = f_id + s_id;
            Ticket t = ticketService.getTicketById(t_id);
            PurchaseRecord pr = new PurchaseRecord(email, t_id, date, time, t.getPrice());
            prList.add(pr.getWcnm());
            purchaseRecordService.addNewPurchaseRecord(pr);
        }
        resMap.put("res", true);
        resMap.put("message", "成功返回");
        resMap.put("List", prList);
        return resMap;
    }
}
